<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function p($arr = [])
{
    echo "<pre>";
    print_r($arr);
    echo "<pre>";
}

function d($arr = [])
{
    p($arr);
    die;
}

/**
 * 检测数组,是否为不为空的数组
 * 是 true 否 false
 * @param $arr
 * @return bool
 */
function checkArr($arr)
{
    $res = is_array($arr) ? count($arr) > 0 ? true : false : false;
    return $res;
}

/**
 * 检测是否非空字符串
 * 是 true 否 false
 * @param $str
 * @return bool
 */
function checkStr($str = '')
{
    $str = trim($str);
    if ($str == '' || $str == null) {
        return false;
    }
    $res = empty($str) ? false : true;
    return $res;
}

/**
 * 二位数组元素替换键值
 * @param array $arr
 * @param string $id
 * @return array|false
 */
function arrayCombine($arr = [], $id = '')
{
    $temp_key = array_column($arr, $id);  //键值 php5.5+
    $mobile_arr = array_combine($temp_key, $arr);
    return $mobile_arr;
}

/**
 * 获取$_SERVER
 * @param string $param
 * @return mixed|string
 */
function server($param = '')
{
    $request = Request::server();
    if (!empty($param)) {
        return isset($request[$param]) ? $request[$param] : '';
    } else {
        return $request;
    }
}

/**
 * 获取$_GET值
 * @param string $param
 * @return mixed
 */
function get($param = '')
{
    $request = Request::get();
    if (!empty($param)) {
        return isset($request[$param]) ? $request[$param] : '';
    } else {
        return $request;
    }
}

/**
 * 获取$_POST值
 * @param string $param
 * @return mixed|string
 */
function post($param = '')
{
    $request = Request::post();
    if (!empty($param)) {
        return isset($request[$param]) ? $request[$param] : '';
    } else {
        return $request;
    }
}

function ipost($param = '')
{
    return (int)post($param);
}

function spost($param = '')
{
    return (string)trim(post($param));
}

function iget($param = '')
{
    return (int)get($param);
}

function sget($param = '')
{
    return (string)trim(get($param));
}


/**
 * 生成数据返回值-服务器对接
 * @param string $msg
 * @param bool $state
 * @param array $data
 * @return array
 */
function returnArr($msg = '', $state = false, $data = [])
{
    return ['state' => $state, 'msg' => $msg, 'data' => $data];
}


/**
 * PKCS1 解密
 * @param $data
 * @param $private_key
 * @param string $decrypted
 * @return string
 */
function decoding($data, $private_key, $decrypted = '')
{
    $search = [
        "-----BEGIN RSA PRIVATE KEY-----",
        "-----END RSA PRIVATE KEY-----",
        "\n",
        "\r",
        "\r\n"
    ];
    $private_key = str_replace($search, "", $private_key);
    $private_key = $search[0] . PHP_EOL . wordwrap($private_key, 64, "\n", true) . PHP_EOL . $search[1];
    $pi_key = openssl_pkey_get_private($private_key);//这个函数可用来判断私钥是否是可用的，可用返回资源id Resource id
    openssl_private_decrypt(base64_decode($data), $decrypted, $pi_key, OPENSSL_PKCS1_PADDING);//私钥解密
    return $decrypted;
}


